package com.dcrzl.shcz.module.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dcrzl.shcz.common.entity.sys.SysRole;
import com.dcrzl.shcz.common.entity.sys.SysRoleGov;
import com.dcrzl.shcz.common.entity.sys.SysRoleMenu;
import com.dcrzl.shcz.common.exception.MyException;
import com.dcrzl.shcz.common.mapper.sys.SysRoleMapper;
import com.dcrzl.shcz.common.service.impl.BaseServiceImpl;
import com.dcrzl.shcz.common.utils.PageInfo;
import com.dcrzl.shcz.common.utils.Result;
import com.dcrzl.shcz.common.utils.SysConstant;
import com.dcrzl.shcz.module.sys.service.SysRoleGovService;
import com.dcrzl.shcz.module.sys.service.SysRoleMenuService;
import com.dcrzl.shcz.module.sys.service.SysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class SysRoleServiceImpl extends BaseServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Autowired
    private SysRoleGovService sysRoleGovService;

    @Override
    public Result<SysRole> selectOne(SysRole sysRole) {
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        return super.selectOne(wrapper);
    }

    @Override
    public Result<List<SysRole>> selectList(SysRole sysRole) {
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        return super.selectList(wrapper);
    }

    @Override
    public PageInfo<SysRole> selectPage(SysRole sysRole) {
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(sysRole.getRoleName()),SysRole::getRoleName,
                "%"+sysRole.getRoleName()+"%");
        return super.selectPage(sysRole, wrapper);
    }

    @Override
    public Result selectURoleMenuById(Long id) {
        Result<SysRole> sysRoleResult = this.selectById(id);
        if(sysRoleResult.isSuccess()){
            SysRole  sysRole = sysRoleResult.getData();
            Result<List<SysRoleMenu>> result = sysRoleMenuService.selectList(new SysRoleMenu().
                    setRoleId(id));
            if (result.isSuccess()){
                List<SysRoleMenu> list = result.getData();
                List<Long> menuIdList = new ArrayList<>();
                list.forEach(sysRoleMenu -> {
                    menuIdList.add(sysRoleMenu.getMenuId());
                });
                sysRole.setMenuIdList(menuIdList);
            }
            Result<List<SysRoleGov>> listResult = sysRoleGovService.selectList(new SysRoleGov().setRoleId(id));
            if (listResult.isSuccess()){
                List<SysRoleGov> list = listResult.getData();
                List<Long> govIdList = new ArrayList<>();
                list.forEach(sysRoleGov -> {
                    govIdList.add(sysRoleGov.getGovId());
                });
                sysRole.setGovIdList(govIdList);
            }
        }
        return sysRoleResult;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Integer> uSave(SysRole sysRole) {
        log.info(sysRole.toString());
        Result<Integer> insert = this.insert(sysRole);
        if(insert.isSuccess()){
            List<Long> menuIdList = sysRole.getMenuIdList();
            List<SysRoleMenu> roleMenuList = new ArrayList<>();
            menuIdList.forEach(menuId->{
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setMenuId(menuId).setRoleId(sysRole.getId())
                        .setCreateTime(new Date()).setStatus(SysConstant.DATA_NORMAL_STATUS);
                roleMenuList.add(sysRoleMenu);
            });
            Result<Integer> integerResult = sysRoleMenuService.insertBatch(roleMenuList);
            if(!integerResult.isSuccess()){
                throw new MyException("添加权限失败");
            }
            if(!CollectionUtils.isEmpty(sysRole.getGovIdList())){
                List<SysRoleGov> sysRoleGovList = new ArrayList<>();
                sysRole.getGovIdList().forEach(govId->{
                    sysRoleGovList.add(new SysRoleGov().setGovId(govId).setRoleId(sysRole.getId()));
                });
                Result<Integer> integerResult1 = sysRoleGovService.insertBatch(sysRoleGovList);
                if(!integerResult1.isSuccess()){
                    throw new MyException("添加行政单位权限范围失败");
                }
            }
        }
        return insert;
    }

    @Override
    public Result uUpdateById(SysRole sysRole) {
        if(sysRole.getId() != 1L){
            //删除角色权限
            Result<List<SysRoleMenu>> listResult = sysRoleMenuService.selectList(new SysRoleMenu().setRoleId(sysRole.getId()));
            if(listResult.isSuccess()){
                List<SysRoleMenu> list = listResult.getData();
                List<Long> idList = new ArrayList<>();
                list.forEach(sysRoleMenu -> {
                    idList.add(sysRoleMenu.getId());
                });
                sysRoleMenuService.deleteBatchIds(idList);
            }
            //删除角色行政单位
            Result<List<SysRoleGov>> listResult1 = sysRoleGovService.selectList(new SysRoleGov().setRoleId(sysRole.getId()));
            if(listResult1.isSuccess()){
                List<SysRoleGov> sysRoleGovList = listResult1.getData();
                List<Long> idList = new ArrayList<>();
                sysRoleGovList.forEach(sysRoleGov->{
                   idList.add(sysRoleGov.getId());
                });
                sysRoleGovService.deleteBatchIds(idList);
            }
        }
        List<Long> menuIdList = sysRole.getMenuIdList();
        List<SysRoleMenu> roleMenuList = new ArrayList<>();
        menuIdList.forEach(menuId->{
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(menuId).setRoleId(sysRole.getId())
                    .setCreateTime(new Date()).setStatus(SysConstant.DATA_NORMAL_STATUS);
            roleMenuList.add(sysRoleMenu);
        });
        Result<Integer> integerResult = sysRoleMenuService.insertBatch(roleMenuList);
        if(!integerResult.isSuccess()){
            throw new MyException("添加权限失败");
        }
        if(!CollectionUtils.isEmpty(sysRole.getGovIdList())){
            List<SysRoleGov> sysRoleGovList = new ArrayList<>();
            sysRole.getGovIdList().forEach(govId->{
                sysRoleGovList.add(new SysRoleGov().setGovId(govId).setRoleId(sysRole.getId()));
            });
            Result<Integer> integerResult1 = sysRoleGovService.insertBatch(sysRoleGovList);
            if(!integerResult1.isSuccess()){
                throw new MyException("添加行政单位权限范围失败");
            }
        }
        return this.updateById(sysRole);
    }


    @Override
    public Result<Integer> removeById(Serializable id) {
        SysRole sysRole = new SysRole().setStatus(SysConstant.DATA_DELETE_STATUS)
                .setId((Long) id);
        return this.updateById(sysRole);
    }
}